#ifndef _DBHELP_H
#define _DBHELP_H

#define YES 1
#define NO  0

typedef char mytime[20];

// help.
#define SAFE_MALLOC(dest, size) do {\
    dest = (typeof(dest))malloc(size);\
    memset(dest, 0, size);}while(0)

// these macros assume that use MYSQL*: conn.
#define SAFE_QUERY_STORE(query, result, fault_ret) do {\
    pthread_mutex_lock(&mutex_mysql);\
    if (mysql_query(conn, query)) {\
	printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));\
	pthread_mutex_unlock(&mutex_mysql);\
	return fault_ret; }\
    result = mysql_store_result(conn);\
    pthread_mutex_unlock(&mutex_mysql); }while(0)

#define SAFE_QUERY_INSERT_ID(query, id, fault_ret) do {\
    pthread_mutex_lock(&mutex_mysql);\
    if (mysql_query(conn, query)) {\
	printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));\
	pthread_mutex_unlock(&mutex_mysql);\
	return fault_ret; }\
    id = mysql_insert_id(conn);\
    pthread_mutex_unlock(&mutex_mysql); }while(0)

#define SAFE_QUERY(query, fault_ret) do {\
    if (mysql_query(conn, query)) {\
	printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));\
	pthread_mutex_unlock(&mutex_mysql);\
	return fault_ret; } }while(0)

#define LOG(query) do{\
    fprintf(stderr, "[log]:<%s>, ", __func__);	\
    fprintf(stderr, query);\
    fprintf(stderr, "\n");\
}while(0)

#endif // _DBHELP_H
